﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.ICM.ViewModel;
using KPIS.GERP.Models.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;

namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface IMasResourceService
    {
        [OperationContract]
        IEnumerable<TreeViewModel> SelectTreeViewResource(MasResourceType criteriaModel = null);//, List<string> includeSubObjectString = null);

        #region MasResourceType
        [OperationContract]
        IEnumerable<MasResourceType> SelectResourceType(MasResourceType criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IList<SelectListItem> SelectListResourceType(MasResourceType criteriaModel = null);

        [OperationContract]
        int InsertResourceType(MasResourceType model, int createByUserId);

        [OperationContract]
        void UpdateResourceType(MasResourceType model, int updateByUserId);

        [OperationContract]
        void DeleteResourceType(int id, int deleteByUserId);
        #endregion

        #region MasResourceGroup
        [OperationContract]
        IEnumerable<MasResourceGroup> SelectResourceGroup(MasResourceGroup criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IList<SelectListItem> SelectListResourceGroup(MasResourceGroup criteriaModel = null);

        [OperationContract]
        int InsertResourceGroup(MasResourceGroup model, int createByUserId);

        [OperationContract]
        void UpdateResourceGroup(MasResourceGroup model, int updateByUserId);

        [OperationContract]
        void DeleteResourceGroup(int id, int deleteByUserId);
        #endregion

        #region MasResourceSubGroup
        [OperationContract]
        IEnumerable<MasResourceSubGroup> SelectResourceSubGroup(MasResourceSubGroup criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IList<SelectListItem> SelectListResourceSubGroup(MasResourceSubGroup criteriaModel = null);

        [OperationContract]
        int InsertResourceSubGroup(MasResourceSubGroup model, int createByUserId);

        [OperationContract]
        void UpdateResourceSubGroup(MasResourceSubGroup model, int updateByUserId);

        [OperationContract]
        void DeleteResourceSubGroup(int id, int deleteByUserId);
        #endregion

        #region MasResourceItem
        [OperationContract]
        IEnumerable<MasResourceItem> SelectResourceItem(MasResourceItem criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        IList<SelectListItem> SelectListResourceItem(MasResourceItem criteriaModel = null);

        [OperationContract]
        int InsertResourceItem(MasResourceItem model, int createByUserId);

        [OperationContract]
        void UpdateResourceItem(MasResourceItem model, int updateByUserId);

        [OperationContract]
        void DeleteResourceItem(int id, int deleteByUserId);
        #endregion
    }

	[ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class MasResourceService : IMasResourceService
    {
        public IEnumerable<TreeViewModel> SelectTreeViewResource(MasResourceType criteriaModel = null)//, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    var x = context.MasResourceTypes.Include("MasResourceGroups")
                        .Include("MasResourceGroups.MasResourceSubGroups")
                        .Include("MasResourceGroups.MasResourceSubGroups.MasResourceItems").ToList();

                    var TreeViewList = new List<TreeViewModel>();

                    foreach (MasResourceType type in x)
                    {
                        var TreeView1 = new TreeViewModel();
                        var Children1 = new List<TreeViewModel>();
                        TreeView1.Id = "type-" + type.ResTypeId.ToString();
                        TreeView1.Name = type.Name;
                        TreeView1.ParentId = null;

                        //foreach (MasResourceGroup grp in type.MasResourceGroups)//.Where(f => f.RecordStatus != RecordStatus.DeletedStatus))
                        //{
                        //    var TreeView2 = new TreeViewModel();
                        //    var Children2 = new List<TreeViewModel>();
                        //    TreeView2.Id = "grp-" + grp.ResGrpId.ToString();
                        //    TreeView2.Name = grp.Name;
                        //    TreeView2.ParentId = "type-" + grp.ResTypeId.ToString();

                        //    foreach (MasResourceSubGroup subGrp in grp.MasResourceSubGroups)//.Where(f => f.RecordStatus != RecordStatus.DeletedStatus))
                        //    {
                        //        var TreeView3 = new TreeViewModel();
                        //        var Children3 = new List<TreeViewModel>();
                        //        TreeView3.Id = "sub-" + subGrp.ResSubGrpId.ToString();
                        //        TreeView3.Name = subGrp.Name;
                        //        TreeView3.ParentId = "grp-" + subGrp.ResGrpId.ToString();

                        //        foreach (MasResourceItem item in subGrp.MasResourceItems)//.Where(f => f.RecordStatus != RecordStatus.DeletedStatus))
                        //        {
                        //            var TreeView4 = new TreeViewModel();
                        //            TreeView4.Id = "item-" + item.ResItemId.ToString();
                        //            TreeView4.Name = item.Name;
                        //            TreeView4.ParentId = "sub-" + item.ResSubGrpId.ToString();

                        //            Children3.Add(TreeView4);
                        //        }

                        //        TreeView3.Children = Children3;
                        //        Children2.Add(TreeView3);
                        //    }

                        //    TreeView2.Children = Children2;
                        //    Children1.Add(TreeView2);
                        //}

                        TreeView1.Children = Children1;
                        TreeViewList.Add(TreeView1);
                    }

                    return TreeViewList;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region MasResourceType
        public IEnumerable<MasResourceType> SelectResourceType(MasResourceType criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceType> query = context.MasResourceTypes;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var xi in x)
                    {
                        xi.AmountOfGroup = context.MasResourceGroups.Count(o => o.RecordStatus != "D"&& o.ResTypeId == xi.ResTypeId);
                    }
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<SelectListItem> SelectListResourceType(MasResourceType criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceType> query = context.MasResourceTypes;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.ResTypeId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int InsertResourceType(MasResourceType model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.UpdatedBy = createByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.MasResourceTypes.Add(model);
                    context.SaveChanges();

                    return model.ResTypeId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateResourceType(MasResourceType model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteResourceType(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasResourceTypes.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region MasResourceGroup
        public IEnumerable<MasResourceGroup> SelectResourceGroup(MasResourceGroup criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceGroup> query = context.MasResourceGroups;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var xi in x)
                    {
                        xi.AmountOfSubGroup = context.MasResourceSubGroups.Count(o => o.RecordStatus != "D"&& o.ResGrpId == xi.ResGrpId);
                    }
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<SelectListItem> SelectListResourceGroup(MasResourceGroup criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceGroup> query = context.MasResourceGroups;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.ResGrpId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int InsertResourceGroup(MasResourceGroup model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.UpdatedBy = createByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.MasResourceGroups.Add(model);
                    context.SaveChanges();

                    return model.ResGrpId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateResourceGroup(MasResourceGroup model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteResourceGroup(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasResourceGroups.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region MasResourceSubGroup
        public IEnumerable<MasResourceSubGroup> SelectResourceSubGroup(MasResourceSubGroup criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceSubGroup> query = context.MasResourceSubGroups;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var xi in x)
                    {
                        xi.AmountOfItem = context.MasResourceItems.Count(o => o.RecordStatus != "D" && o.ResSubGrpId == xi.ResSubGrpId);
                    }
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<SelectListItem> SelectListResourceSubGroup(MasResourceSubGroup criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceSubGroup> query = context.MasResourceSubGroups;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.ResSubGrpId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int InsertResourceSubGroup(MasResourceSubGroup model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.UpdatedBy = createByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.MasResourceSubGroups.Add(model);
                    context.SaveChanges();

                    return model.ResSubGrpId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateResourceSubGroup(MasResourceSubGroup model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteResourceSubGroup(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasResourceSubGroups.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region MasResourceItem
        public IEnumerable<MasResourceItem> SelectResourceItem(MasResourceItem criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceItem> query = context.MasResourceItems;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<SelectListItem> SelectListResourceItem(MasResourceItem criteriaModel = null)
        {
            var list = new List<SelectListItem>();
            list.Add(new SelectListItem { Selected = true, Text = "ไม่ระบุ", Value = "" });

            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasResourceItem> query = context.MasResourceItems;

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach (var model in x)
                    {
                        var item = new SelectListItem { Text = model.Name, Value = model.ResItemId.ToString() };
                        list.Add(item);
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
        }

        public int InsertResourceItem(MasResourceItem model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.UpdatedBy = createByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.MasResourceItems.Add(model);
                    context.SaveChanges();

                    return model.ResItemId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateResourceItem(MasResourceItem model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteResourceItem(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasResourceItems.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
